<?php

namespace addons\cms\controller\wxapp;

use addons\cms\model\Archives;
use addons\cms\model\Block;
use addons\cms\model\Channel;
use app\common\model\Addon;
use addons\cms\model\Comment;
use addons\cms\model\User_follow;
use app\common\library\Auth;

/**
 * 首页
 */
class Index extends Base
{

    protected $noNeedLogin = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 首页
     */
    public function index()
    {
        $bannerList = [];
        $list = Block::getBlockList(['name' => 'focus', 'row' => 5]);
        foreach ($list as $index => $item) {
            $bannerList[] = ['image' => cdnurl($item['image'], true), 'url' => '/', 'title' => $item['title']];
        }

        $tabList = [
           // ['id' => 0, 'title' => '全部'],
        ];
        $channelList = Channel::where('status', 'normal')
            ->where('type', 'in', ['channel'])
            ->field('id,parent_id,name,diyname')
            ->order('weigh desc,id desc')
            ->cache(false)
            ->select();
        foreach ($channelList as $index => $item) {
            $tabList[] = ['id' => $item['id'], 'title' => $item['name']];
        }

        $tabList2 = Channel::where('status', 'normal')
            ->where('parent_id', $tabList[0]['id'])
            ->where('type', 'in', ['list'])
            ->order('weigh desc,id desc')
            ->cache(false)
            ->select();
        $tabList2 = collection($tabList2)->toArray();
        $tabList2_count = count($tabList2);
        foreach ($tabList2 as $k=>$v){
            $image= 'https://'.$_SERVER['SERVER_NAME'].$v['image'];
            $tabList2[$k]['image'] = $image;
        }
        $archivesList = Archives::getArchivesList(['channel'=>$tabList[0]['id']]);

        //获取最新三条评论
        foreach ($archivesList as $k=>$v){
            if(substr($v['user']['avatar'],0,4) != 'http'){
                $avatar = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].$v['user']['avatar'];
                $archivesList[$k]['user']['avatars'] = $avatar;
            }else{
                $archivesList[$k]['user']['avatars'] = $v['user']['avatar'];;
            }

            if(substr($v['image'],strrpos($v['image'],'/')+1) == 'noimage.jpg'){
                unset($archivesList[$k]['image']);
            }

            $channelParent = Channel::where('id',$v['channel']['parent_id'])->find();
            if ($channelParent) {
                $archivesList[$k]['parentNeme'] = $channelParent['name'];
            }
            $commentList = Comment::getCommentList(['aid' => $v['id']]);
            $archivesList[$k]['comment'] = $commentList->getCollection();

            $auth = Auth::instance();
            $is_follow = User_follow::where('user_id', $auth->id)
                ->where('follow_id', $v['user_id'])
                ->select();

            if(!empty($is_follow)){
                $archivesList[$k]['user']['follow'] = '取消关注';
            }else{
                $archivesList[$k]['user']['follow'] = '关注';
            }
        }

        $data = [
            'bannerList'   => $bannerList,
            'tabList'      => $tabList,
            'tabList2'      => $tabList2,
            'tabList2_count'      => $tabList2_count,
            'archivesList' => $archivesList,
        ];
        $this->success('', $data);

    }
}
